<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Contains examples of various Kohana library examples. You can access these
 * samples in your own installation of Kohana by going to ROOT_URL/examples.
 * This controller should NOT be used in production. It is for demonstration
 * purposes only!
 *
 * $Id: examples.php 4134 2009-03-28 04:37:54Z zombor $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */

class Feedback_Controller extends Web_Controller {

	// Do not allow to run in production
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'kohana/template';
	/**
	 * Displays a list of available examples
	 */
	function index()
	{
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('feedback');
		$this->template->selected_page = null;;
		$this->all_count = getAllCount();
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = Kohana::lang('feedback.main_title');;

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
 
		if(request::is_ajax()){


			$params = array(
				'fd_title' => trim(urldecode($_REQUEST['title'])),
				'fd_text' => trim(urldecode($_REQUEST['msg'])),
				'fd_name' => trim(urldecode($_REQUEST['name'])),
				'fd_email' => trim(urldecode($_REQUEST['mail']))
			);
					
			if(empty($params['fd_title']) || empty($params['fd_text']) || empty($params['fd_name']) || empty($params['fd_email'])){
				
				echo  Kohana::lang('feedback.empty_fills');;
				exit();
				
			}else{
 
					if (!valid::email($params['fd_email'])) {
						echo  Kohana::lang('feedback.wrong_main');;
						exit();
					}
					
					$db = new Database;
 
					$db->query('SET CHARACTER SET utf8');
					$db->query('SET NAMES utf8');
					$db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
 
					$status = $db->insert('feedback', $params);
					 
					// count how many rows were inserted
					$rows = count($status);
					
					if ($rows > 0) {
						echo Kohana::lang('feedback.request_result');
					}else {
						echo  Kohana::lang('feedback.request_error');;
					}
					
					exit();
					
			}
				
		}
	}
 
} ?>
